package io.renren.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.CourseContentEntity;
import io.renren.modules.admin.entity.CourseEntity;
import io.renren.modules.admin.entity.CourseVideoEntity;
import io.renren.modules.admin.service.CourseContentService;
import io.renren.modules.admin.service.CourseService;
import io.renren.modules.admin.service.CourseVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;


/**
 * 课程表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-12-17 18:24:00
 */
@RestController
@RequestMapping("admin/course")
@Api("课程表")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseVideoService courseVideoService;

    @Autowired
    private CourseContentService courseContentService;

    /**
     *  教师课程 列表
     */
    @GetMapping("/list")
    @ApiImplicitParams(
            {@ApiImplicitParam(name="type",value="0:课程,1:上下册,2:课程栏目",paramType="query"), @ApiImplicitParam(name="fId",value="父Id",paramType="query")}
    )
    @ApiOperation("查询课程列表")
    public R list(@ApiIgnore @RequestParam Map<String, Object> params){
        List<CourseEntity> courseEntities = courseService.selectByWrapper(params);
        courseEntities.forEach(entity->entity.setHasChildren(true));
        return R.ok().put("list", courseEntities);
    }

    /**
     * 查询 全部列表
     */
    @GetMapping("all/courseList")
    public R allCourseList(){
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        //wrapper.eq("side","1");
        List<CourseEntity> list = courseService.list();

        return new R().put("list",list);
    }

    /**
     * 查询学生课程列表
     */
    @GetMapping("stu/courseList")
    public R stuCourseList(Integer level){
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("side","1");
        if(level!=null ){
           wrapper.eq("type","0");
        }
        List<CourseEntity> list = courseService.list(wrapper);

        return new R().put("list",list);
    }

    /**
     * 课程和上下册列表
     */
    @GetMapping("/smallList/{level}")
    public R smallList(@PathVariable("level") String level){
       QueryWrapper<CourseEntity> queryWrapper = new QueryWrapper<>();
       if(level.equals("1")){
           queryWrapper.eq("type","0");
       }else if(level.equals("2")){
           queryWrapper.eq("type","1").or().eq("type","0");
       }
        queryWrapper.eq("side","0");
        List<CourseEntity> list = courseService.list(queryWrapper);
        return  new R().put("list",list);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{courseId}")
    @RequiresPermissions("admin:course:info")
    public R info(@PathVariable("courseId") Long courseId){
		CourseEntity course = courseService.getById(courseId);
        return R.ok().put("course", course);
    }

    /**
     * 保存 课程
     */
    @RequestMapping("/save")
    @PostMapping("admin:course:save")
    public R save(@RequestBody CourseEntity course){
       if(course.getFId()==null){
           course.setFId(0L);//
       }
		courseService.createCourse(course);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:course:update")
    public R update(@RequestBody CourseEntity course){
		courseService.updateById(course);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{courseId}")
    @RequiresPermissions("admin:course:delete")
    public R delete(@PathVariable("courseId") Long courseId){
		//
        // ！！！没有批量删除
        QueryWrapper<CourseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("f_id",courseId);
        //如果有 上下册
        List<CourseEntity> list = courseService.list(wrapper);
       if(null!=list && list.size()!=0){
           return R.error("请先删除子级");
       }

       //如果删除 的是 课程 判断 课程视频
        CourseEntity courseEntity = courseService.getById(courseId);
        if(courseEntity.getType()==0){
            //查询课程视频
            QueryWrapper<CourseVideoEntity> courseVideoWrapper = new QueryWrapper<>();
            courseVideoWrapper.eq("course_id",courseEntity.getCourseId());
            List<CourseVideoEntity> video = courseVideoService.list(courseVideoWrapper);
            if(null!=null && video.size() !=0){
                return  R.error("请先删除课程视频");
            }
        }else if(courseEntity.getType()==2){
            //如果删除课程栏目 要判断 课程 内容
            QueryWrapper<CourseContentEntity> courseContentEntityWrapper=new QueryWrapper();
            courseContentEntityWrapper.eq("course_id",courseEntity.getCourseId());
            List<CourseContentEntity> CourseContentEntities = courseContentService.list(courseContentEntityWrapper);
            if(null != CourseContentEntities && CourseContentEntities.size() != 0){
                return R.error("请先删除课程内容");
            }
        }

        courseService.removeById(courseId);
        return R.ok();
    }

}
